package com.cungmua.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cungmua.app.R;
import com.cungmua.app.base.BaseAuthenticatedActivity;
import com.cungmua.app.model.Cart;

/**
 * Created by kelvin.nguyen on 6/25/14.
 */
public class PaymentActivity extends BaseAuthenticatedActivity {

    private WebView webPayment;
    private String paymentUrl;
    private String resultUrl;
    private long orderId;
    private ImageView icon_home;
    private Button icon_cart;
    private LinearLayout btnCart;



    @Override
    public int getLayoutResource() {
        return R.layout.activity_webview;
    }

    @Override
    public void updateView() {
        paymentUrl = getIntent().getExtras().getString(IK_PAYMENT_URL);
        resultUrl = getIntent().getExtras().getString(IK_RESULT_URL);
        orderId = getIntent().getExtras().getLong(IK_ORDER_ID);

        webPayment.setInitialScale(100);
        webPayment.getSettings().setJavaScriptEnabled(true);
        webPayment.getSettings().setUseWideViewPort(true);
        webPayment.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.contains("ok://cungmua?")) {
                    if (url.indexOf("0") >= 0 && url.indexOf("success") >= 0) { // success
                        Intent intent = new Intent();
                        intent.putExtras(getIntent().getExtras());
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        setResult(RESULT_CANCELED);
                        finish();
                    }
                    return false;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        webPayment.loadUrl(paymentUrl);

        icon_home=(ImageView)findViewById(R.id.icon_home);
        icon_cart=(Button)findViewById(R.id.icon_cart);
        btnCart=(LinearLayout)findViewById(R.id.btnCart);


        Cart currentCart=getCurrentCart();
        icon_cart.setText(currentCart.getTotalQuantity()+"");

        icon_home.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu();
            }
        });

        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToYourCart();
            }
        });

    }

    @Override
    public void loadControls(Bundle savedInstanceState) {
        webPayment = (WebView) findViewById(R.id.webView);
    }
}

